package com.wl.week_01;

import java.util.*;

/**
 * 智慧教辅系统 简单登陆系统的实现
 * @author TaiZai
 * @data 2024/9/23 17:23
 */
public class Demo_2 {
    private static List<User> usersList = new ArrayList<>();
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        /*
            智慧教辅系统登陆注册
         */
        while(true) {
            System.out.println("欢迎来到智慧教辅系统，请进行下列选择");
            System.out.println("1、登陆");
            System.out.println("2、注册");
            System.out.println("3、展示用户");
            System.out.println("0、退出");
            int choice = sc.nextInt();
            gotoPage(choice);
        }
    }

    public static void Login() {
        System.out.print("请输入用户名：");
        String username = sc.nextLine();
        System.out.print("请输入密码：");
        String password = sc.nextLine();

        if (findUser(username, password) == 1) {
            System.out.println("登陆成功!");
        } else if (findUser(username, password) == 0) {
            System.out.println("密码错误!");
        } else {
            System.out.println("该用户不存在!");
            gotoPage(1);
        }
    }

    public static void Registered() {
        System.out.print("请输入您的用户名：");
        String username = sc.nextLine();
        System.out.print("请输入您的新密码：");
        String password = sc.nextLine();

        if (username.isEmpty() || password.isEmpty()) {
            System.out.println("用户名或密码不能为空!");
            gotoPage(2);
            return;
        }

        String usernameReg = "[A-Z][0-9]{4,8}[a-z]";

        if (username.length() < 6 || username.length() > 10) {
            System.out.println("用户名的长度为(6,10)");
            gotoPage(2);
            return;
        }

        String passwordReg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-]";
        if (password.length() < 8 || password.length() > 12) {
            System.out.println("密码的长度为(8,12)");
            gotoPage(2);
            return;
        }

        if (findUser(username, password) != -1) {
            System.out.println("该用户名已存在!");
            gotoPage(2);
        } else {
            usersList.add(new User(username, password));
            System.out.println("注册成功!");
        }
    }

    public static void printAllUserInfo() {
        int cnt = 1;
        for (User u : usersList) {
            System.out.println(cnt++ + "、用户名："+u.getUsername()+", 密码："+u.getPassword());
        }
    }
    public static Integer findUser(String username, String password) { // 返回 1 登陆成功, 0 密码错误, -1 用户名不存在
        for (User u : usersList) {
            if (u.getUsername().equals(username)) {
                return u.getPassword().equals(password) ? 1 : 0;
            } else {
                return -1;
            }
        }
        return -1;
    }


    public static void gotoPage(int page) {
        switch (page) {
            case 1 -> Login();
            case 2 -> Registered();
            case 3 -> printAllUserInfo();
            case 0 -> System.exit(0);
            default -> System.out.println("请输入正确的操作数!");
        }
    }
}
